package com.depin.pdaapp.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Message;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.depin.pdaapp.R;
import com.depin.pdaapp.event.MyEvent;
import com.jaydenxiao.common.base.BaseActivity;
import com.jaydenxiao.common.commonutils.ToastUitl;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

public class ScanActivity extends BaseActivity {

    /**
     * 二维码解析回调函数
     */
    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            EventBus.getDefault().post(new MyEvent.MessageEvent(result));
            Message obtain = Message.obtain();
            obtain.what = R.id.restart_preview;
            captureFragment.getHandler().sendMessageDelayed(obtain, 500);
        }

        @Override
        public void onAnalyzeFailed() {
            ToastUitl.showShort("二维码不支持");
        }
    };
    @BindView(R.id.fl_my_container)
    FrameLayout flMyContainer;
    @BindView(R.id.tv_bar)
    TextView tvBar;
    @BindView(R.id.iv_notification)
    ImageView ivNotification;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.activity_second)
    FrameLayout activitySecond;
    CaptureFragment captureFragment = new CaptureFragment();

    @Override
    public int getLayoutId() {
        return R.layout.activity_scan;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {

        tvBar.setText("扫码");
        /**
         * 执行扫面Fragment的初始化操作
         */
        // 为二维码扫描界面设置定制化界面
        CodeUtils.setFragmentArgs(captureFragment, R.layout.my_camera);

        captureFragment.setAnalyzeCallback(analyzeCallback);

        /**
         * 替换我们的扫描控件
         */
        getSupportFragmentManager().beginTransaction().replace(R.id.fl_my_container, captureFragment).commit();
    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
